//
//  PurchaseContactDetailViewController.swift
//  HuaYun
//
//  Created by enghou on 2018/8/30.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit

class PurchaseContactDetailViewController: ApprovalDetailViewController {

    override func path(for name: String) -> String {
        return PurchaseContactOrderDetail
    }
    
    override func success(for name: String) -> NetSuccess {
        func suc(json: JSON)->Void{

            if json.contents?["success"]?.bool == false {
                self.alert(json.contents?["errorMsg"]?.rawString())
                self.pop()
                return
            }

            let data = json.contents?["data"]?.contents

            let details = data

            let purchase = PurchaseTableViewController()

            var contents: Array<Dictionary<String,String?>> = []

            contents.append(["采购单号:":details?["serialNumber"]?.rawString()])

            contents.append(["采购任务名称:":details?["taskName"]?.rawString()])

            if let type = details?["type"]?.rawString() {

                if type == "serviceEngineering" {
                    contents.append(["采购类型":"服务类"])
                } else if type == "humanResources" {
                    contents.append(["采购类型":"人力类"])
                } else if type == "engineering" {
                    contents.append(["采购类型":"工程类"])
                } else if type == "material" {
                    contents.append(["采购类型":"物资类"])
                }

            } else {
                contents.append(["采购类型":"未知"])
            }

            contents.append(["采购依据:":details?["purchaseBasisName"]?.rawString()])

            contents.append(["需求日期:":details?["demandTime"]?.rawString()])

            if let amount = details?["estimatedAmount"]?.rawValue as? Int {

                let rmb = amount / 100

                contents.append(["预算金额(元):":String(rmb).rmbformat])

            } else {

                contents.append(["预算金额(元):":""])

            }

            if let ifFrame = details?["whetherFrame"]?.rawValue as? Bool {
                if ifFrame == true {
                    contents.append(["是否框架:":"是"])
                } else {
                    contents.append(["是否框架:":"否"])
                }
            } else {
                contents.append(["是否框架:":"否"])
            }

            contents.append(["建议采购方式:":details?["procurementMethod"]?.rawString()])

            contents.append(["暂定包数:":details?["provisionalPacketNumber"]?.rawString()])

            contents.append(["付款方式:":details?["paymentMethod"]?.rawString()])

            let incomingAmount = details?["incomingAmount"]?.int ?? 0
            contents.append(["进项金额(元):":String(incomingAmount).rmbformat])

            contents.append(["质保期(月):":details?["warrantyPeriod"]?.rawString()])
        
            contents.append(["服务期限:":details?["servicePeriod"]?.rawString()])
            
            contents.append(["申请日期":details?["applyTime"]?.rawString()])
            
            contents.append(["申请人":details?["applicantInfo"]?.contents?["name"]?.rawString()])
            
            contents.append(["申请部门:":details?["projectDept"]?.rawString()])
            
            contents.append(["专用资质条件:":details?["qualificationConditions"]?.rawString()])
            
            contents.append(["采购背景":details?["projectBackground"]?.rawString()])
            

            purchase.source = contents

            var attachs: Array<JSON> = []

            guard let attachments = details?["attachFiles"]?.array else {
                return
            }
            for item in attachments {
                attachs.append(item)
            }

            purchase.documents = attachs

            ctls.append(purchase)

            self.page.setViewControllers([purchase], direction: .forward, animated: false, completion: nil)
            
            /*********************第一个搞定*********************************/
            
            let type = details?["type"]?.rawString()

            if type != "humanResources" {
                //3ge标签

                slide.configTitles(["采购信息","关联信息","审批记录"])

                let projectInfo = ProjectAssociateViewController()
                
                projectInfo.projName = details?["projectName"]?.rawString()
                
                projectInfo.projSerialNum = details?["proSerialNumber"]?.rawString()
                
                projectInfo.projManager = details?["projManagerName"]?.rawString()
                
                projectInfo.purchaseItemName = details?["purchaseItemName"]?.rawString()
                
                projectInfo.purchasePredicateNum = String((details?["estimatedAmount"]?.int ?? 0) / 100).rmbformat
                
                if let contracts = details?["purchasingDemands"]?.array {
                    
                    if contracts.count > 0 {
                        
                        let contract = contracts[0]
                        projectInfo.contractName = contract.contents?["purchaseContract"]?.contents?["contractName"]?.rawString()
                        projectInfo.contractNum = contract.contents?["purchaseContract"]?.contents?["contractNum"]?.rawString()
                        let money = contract.contents?["purchaseContract"]?.contents?["signingMoney"]?.int ?? 0
                        projectInfo.contractMoney = String(money / 100).rmbformat
                    }
                }
                ctls.append(projectInfo)
            } else {
                 slide.configTitles(["采购信息","审批记录"])
            }

            let approvalList = ApprovalRecordListViewController()

            if let list = data?["printApprovalProcess"]?.array {

                approvalList.source = list

            }

            approvalList.tableView.reloadData()

            ctls.append(approvalList)


        }
        return suc
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.approvalBtn.removeFromSuperview()
        // Do any additional setup after loading the view.
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        page.view.frame = CGRect(origin: CGPoint(x: 0, y: slide.bottom), size: CGSize(width: self.view.width, height: self.view.height - slide.height))
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
